<template>
    <el-dialog class="add-budget" :title="`上传会签确认函`" :visible.sync="visible" :before-close="close" width="600px">
        <el-form :model="formData" :rules="rules" ref="formRef" label-width="140px">
            <el-form-item label="审核结果" prop="status">
                <el-radio :label="1" v-model="formData.status">同意</el-radio>
                <el-radio :label="2" v-model="formData.status">拒绝</el-radio>
            </el-form-item>
            <el-form-item label="拒绝原因" prop="refuse_reason" v-if="formData.status == 2">
                <el-input type="textarea" v-model="formData.refuse_reason" :rows="5" placeholder="请输入拒绝原因"></el-input>
            </el-form-item>
            <el-form-item label="打款凭证" prop="pay_invoice" v-if="formData.status == 1">
                <elUploadImage :limit="1" :multiple="false" v-model="formData.pay_invoice"></elUploadImage>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button type="primary" @click="submit">确认</el-button>
            <el-button @click="close">关闭</el-button>
        </div>
    </el-dialog>
</template>
<script>
import elUploadImage from '@/components/el-upload-image.vue';
import _ from 'lodash'
export default {
    data() {
        return {
            formData: {
                status: 1,
                refuse_reason: '',
                pay_invoice: '',
            },
            rules: {
                status: [{ required: true }],
                refuse_reason: [{ required: true, message: '请输入拒绝原因', trigger: 'blur' }],
                pay_invoice: [{
                    type: 'array', required: true, message: '请上传打款凭证', trigger: 'change'
                }]
            },
            visible: false,
        }
    },
    props: {
        value: {
            type: Object,
            default: null
        }
    },
    watch: {
        value(val) {
            this.visible = val ? true : false
        }
    },
    components: { elUploadImage },
    methods: {
        close() {
            this.$refs.formRef.resetFields()
            this.$emit('input', null)
        },
        async submit() {
            this.$refs.formRef.validate(async(valid) => {
                if (valid) {
                    try {
                        let formData = _.cloneDeep(this.formData)
                        if (formData.status == 1 && formData.pay_invoice[0]) {
                            formData.pay_invoice = formData.pay_invoice[0].url
                        } else {
                            formData.pay_invoice = null
                        }
                        await this.$post({
                            url: '/admin/finance.ProjectPayout/payoutCheck',
                            data: { ...formData, ...{ id: this.value.id } },
                            successTip: true
                        })
                        this.$emit('update')
                        this.close()
                    } catch (err) {
                        this.$message.error(err)
                    }
                }
            })
        }
    }
}
</script>